package com.promise.jokerdream.work.signin.before

import com.promise.jokerdream.manager.ExecutionHistoryManager
import com.promise.jokerdream.repository.GameApi
import com.promise.jokerdream.repository.foldWithApiException
import com.promise.jokerdream.utils.AppContext
import com.promise.jokerdream.work.BaseWork
import com.promise.jokerdream.work.WorkResult

// 每日奖励工作
class DailyGiftWork(uin: String): BaseWork(uin) {

    override val name: String
        get() = "每日奖励"

    override val priority: Int
        get() = 1

    private val gameApi = GameApi.getInstance()
    private val historyManager by lazy { ExecutionHistoryManager.getInstance(AppContext.get()) }

    override suspend fun executeWork(): WorkResult {
        try {
            // 先查询每日奖励状态
            val queryResult = gameApi.queryDailyGift()
            return queryResult.foldWithApiException(
                onSuccess = { response ->
                    // 根据查询结果决定是否领取
                    if (response.login.status == 0) {
                        val loginResult = gameApi.getDailyGift(response.login.key)
                        loginResult.foldWithApiException(
                            onSuccess = { giftResponse ->
                                historyManager.addHistory(name, giftResponse.getDescription())
                            },
                            onFailure = { code, msg ->
                                historyManager.addHistory(name, msg, code)
                            }
                        )
                    }
                    
                    if (response.meridian.status == 0) {
                        val meridianResult = gameApi.getDailyGift(response.meridian.key)
                        meridianResult.foldWithApiException(
                            onSuccess = { giftResponse ->
                                historyManager.addHistory(name, giftResponse.getDescription())
                            },
                            onFailure = { code, msg ->
                                historyManager.addHistory(name, msg, code)
                            }
                        )
                    }
                    
                    if (response.daren.status == 0) {
                        val darenResult = gameApi.getDailyGift(response.daren.key)
                        darenResult.foldWithApiException(
                            onSuccess = { giftResponse ->
                                historyManager.addHistory(name, giftResponse.getDescription())
                            },
                            onFailure = { code, msg ->
                                historyManager.addHistory(name, msg, code)
                            }
                        )
                    }
                    
                    if (response.wuZiTianShu.status == 0) {
                        val wuzitianshuResult = gameApi.getDailyGift(response.wuZiTianShu.key)
                        wuzitianshuResult.foldWithApiException(
                            onSuccess = { giftResponse ->
                                historyManager.addHistory(name, giftResponse.getDescription())
                            },
                            onFailure = { code, msg ->
                                historyManager.addHistory(name, msg, code)
                            }
                        )
                    }
                    
                    // 检查并领取浩劫宝箱
                    val havocTreasureResult = gameApi.queryHavocTreasure()
                    havocTreasureResult.foldWithApiException(
                        onSuccess = { havocResponse ->
                            if (havocResponse.giftFlag == 0) {
                                val claimResult = gameApi.claimHavocTreasure()
                                claimResult.foldWithApiException(
                                    onSuccess = { claimResponse ->
                                        historyManager.addHistory(name, claimResponse.getDescription())
                                    },
                                    onFailure = { code, msg ->
                                        historyManager.addHistory(name, msg, code)
                                    }
                                )
                            }
                        },
                        onFailure = { code, msg ->
                            historyManager.addHistory(name, msg, code)
                        }
                    )
                    
                    WorkResult.Success
                },
                onFailure = { code, msg ->
                    historyManager.addHistory(name, msg, code)
                    WorkResult.Failure(Exception(msg), msg)
                }
            )
        } catch (e: Exception) {
            historyManager.addHistory(name, e.message)
            return WorkResult.Failure(e, e.message)
        }
    }
}